<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use DateTimeInterface;

class Corpuser extends Model
{
    protected $table = 'corp_user';
    protected $primaryKey = 'id';

    public static function corpUserAdd($openid,$corp_id,$name,$phone,$license,$idcard,$student_id,$employee_id,$remark,$avatar,$status)
    {
        return self::insertGetId(["openid"=>$openid,"corp_id"=>$corp_id,"name"=>$name,"phone"=>$phone,"license"=>$license,"idcard"=>$idcard,"student_id"=>$student_id,"employee_id"=>$employee_id,"remark"=>$remark,"avatar"=>$avatar,"status"=>$status,"created_at"=>time()]);
    }

    public static function corpUserDetial($openid,$corp_id)
    {
        return self::where("openid",$openid)->where("corp_id",$corp_id)->first();
    }

    public static function corpUserCount($corp_id)
    {
        return self::where("corp_id",$corp_id)->where("status",2)->count("id");
    }

    public static function corpUserUpdate($openid,$corp_id,$name,$phone,$license,$idcard,$student_id,$employee_id,$remark,$avatar,$status)
    {
        return self::where("openid",$openid)->where("corp_id",$corp_id)->update(["name"=>$name,"phone"=>$phone,"license"=>$license,"idcard"=>$idcard,"student_id"=>$student_id,"employee_id"=>$employee_id,"remark"=>$remark,"avatar"=>$avatar,"status"=>$status]);
    }

    public static function userCorpList($corp_id)
    {
        return self::select('corp_user.created_at','corp_user.status','corp_list.name as corp_name','corp_list.name','corp_list.avatar','corp_user.corp_id')
            ->leftjoin('corp_list', 'corp_list.corp_id', '=', 'corp_user.corp_id')
            ->where("corp_user.corp_id", $corp_id)
            ->where("corp_user.status","!=",5)
            ->orderBy("corp_user.id","DESC")
            ->get();
    }

    public static function corpUserList($corp_id)
    {
        return self::select('openid','name','avatar','phone','license','idcard','student_id','employee_id','remark','status')
            ->where("corp_id", $corp_id)
            ->where("status","!=",5)
            ->orderBy("id","DESC")
            ->simplePaginate(10);
    }

    public static function corpCheckRecord($corp_id,$day)
    {
        return self::select("id","corp_id","name","avatar","phone","created_at")
                    ->where("corp_id",$corp_id)
                    ->where("day",$day)
                    ->simplePaginate(10);
    }

    public static function corpListUpdate($openid,$corp_id,$name,$avatar,$collect,$location,$address)
    {
        return self::where("openid",$openid)->where("corp_id",$corp_id)->update(["name"=>$name,"avatar"=>$avatar,"collect"=>$collect,"address"=>$address]);
    }

    public static function userCorpListDel($openid,$corp_id,$status)
    {
        return self::where("openid",$openid)->where("corp_id",$corp_id)->update(["status"=>$status]);
    }

    public static function checkRecordDetial($openid,$record_id)
    {
        return self::select('corp_user.*','corp_list.name as corp_name','corp_list.collect','corp_list.notice')
            ->leftjoin('corp_list', 'corp_list.corp_id', '=', 'corp_user.corp_id')
            ->where("corp_user.openid", $openid)
            ->where("corp_user.record_id", $record_id)
            ->orderBy("id","DESC")
            ->first();
    }

    public static function checkRecordUser($openid)
    {
        return self::select('corp_user.*','corp_list.name as corp_name','corp_list.collect','corp_list.notice')
            ->leftjoin('corp_list', 'corp_list.corp_id', '=', 'corp_user.corp_id')
            ->where("corp_user.openid", $openid)
            ->orderBy("id","DESC")
            ->first();
    }

    public static function corpUserStatus($openid,$corp_id,$status,$status_msg)
    {
        return self::where("openid",$openid)->where("corp_id",$corp_id)->update(["status"=>$status,"status_msg"=>$status_msg]);
    }
}